<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<h1>Authentication</h1>

<h4>
  By creating identity provider, you are providing capabilities to users to login into the portal / management-ui using external user
  accounts from GitHub, Google, OpenID Connect server or Gravitee.io AM.
</h4>

<mat-card class="org-settings-identity-providers__config-card">
  <mat-card-content>
    <h2>Configuration</h2>
    <mat-slide-toggle
      [checked]="consoleSettings?.authentication?.localLogin?.enabled"
      [disabled]="isReadonlySetting('console.authentication.localLogin.enabled') || !hasActivatedIdp()"
      [matTooltip]="getLocalLoginTooltipMessage()"
      [matTooltipDisabled]="getLocalLoginTooltipMessage() === null"
      (change)="updateLocalLogin($event.checked)"
      >Show login form on management console
    </mat-slide-toggle>
  </mat-card-content>
</mat-card>

<mat-card class="org-settings-identity-providers__list-card">
  <mat-card-content>
    <div class="org-settings-identity-providers__list-card__title">
      <h2>Identity Providers</h2>

      <button [routerLink]="'new'" mat-raised-button color="primary"><mat-icon>add</mat-icon>Add an identity provider</button>
    </div>
  </mat-card-content>

  <gio-table-wrapper [length]="tableUnpaginatedLength" (filtersChange)="onFiltersChanged($event)">
    <table
      mat-table
      [dataSource]="filteredTableData"
      class="org-settings-identity-providers__table"
      id="identityProvidersTable"
      aria-label="Identity Providers table"
    >
      <!-- Logo Column -->
      <ng-container matColumnDef="logo">
        <th mat-header-cell *matHeaderCellDef id="logo"></th>
        <td mat-cell *matCellDef="let element">
          <img class="org-settings-identity-providers__table__logo" [src]="element.logo" alt="Logo of {{ element.name }}" />
        </td>
      </ng-container>

      <!-- Id Column -->
      <ng-container matColumnDef="id">
        <th mat-header-cell *matHeaderCellDef id="id">Id</th>
        <td mat-cell *matCellDef="let element" [routerLink]="element.id">
          <a>{{ element.id }}</a>
        </td>
      </ng-container>

      <!-- Name Column -->
      <ng-container matColumnDef="name">
        <th mat-header-cell *matHeaderCellDef id="name">Name</th>
        <td mat-cell *matCellDef="let element">
          {{ element.name }}
        </td>
      </ng-container>

      <!-- Description Column -->
      <ng-container matColumnDef="description">
        <th mat-header-cell *matHeaderCellDef id="description">Description</th>
        <td mat-cell *matCellDef="let element">{{ element.description }}</td>
      </ng-container>

      <!-- Activated Column -->
      <ng-container matColumnDef="activated">
        <th mat-header-cell *matHeaderCellDef id="activated">Status</th>
        <td mat-cell *matCellDef="let element">
          <span *ngIf="element.activated" class="gio-badge-success">Activated</span>
          <span *ngIf="!element.activated" class="gio-badge-warning">Deactivated</span>
        </td>
      </ng-container>

      <!-- Sync Column -->
      <ng-container matColumnDef="sync">
        <th mat-header-cell *matHeaderCellDef id="sync">
          <div class="org-settings-identity-providers__table__centered-cell">Sync</div>
        </th>
        <td mat-cell *matCellDef="let element">
          <div class="org-settings-identity-providers__table__centered-cell">
            <mat-icon *ngIf="element.sync">check</mat-icon>
          </div>
        </td>
      </ng-container>

      <!-- Available on the Portal Column -->
      <ng-container matColumnDef="availableOnPortal">
        <th mat-header-cell *matHeaderCellDef id="availableOnPortal">
          <div class="org-settings-identity-providers__table__centered-cell">Available on dev portal</div>
        </th>
        <td mat-cell *matCellDef="let element">
          <div class="org-settings-identity-providers__table__centered-cell">
            <mat-icon *ngIf="element.availableOnPortal">check</mat-icon>
          </div>
        </td>
      </ng-container>

      <!-- Updated at Column -->
      <ng-container matColumnDef="updatedAt">
        <th mat-header-cell *matHeaderCellDef id="updatedAt">Updated at</th>
        <td mat-cell *matCellDef="let element">
          {{ element.updatedAt | date: 'medium' }}
        </td>
      </ng-container>

      <!-- Actions Column -->
      <ng-container matColumnDef="actions">
        <th mat-header-cell *matHeaderCellDef id="actions"></th>
        <td mat-cell *matCellDef="let element">
          <div class="org-settings-identity-providers__table__centered-cell">
            <ng-container *gioPermission="{ anyOf: ['organization-identity_provider_activation-u'] }">
              <button
                (click)="onActivationToggleActionClicked(element)"
                mat-icon-button
                aria-label="Switch to toggle an identity provider activation"
                matTooltip="
            {{ element.activated ? 'Deactivate identity provider' : 'Activate identity provider' }}
            "
              >
                <mat-icon>{{ element.activated ? 'toggle_off' : 'toggle_on' }}</mat-icon>
              </button>
            </ng-container>
            <button
              [routerLink]="element.id"
              mat-icon-button
              aria-label="Button to edit an identity provider"
              matTooltip="Edit identity provider"
            >
              <mat-icon>edit</mat-icon>
            </button>
            <ng-container *gioPermission="{ anyOf: ['organization-identity_provider-d'] }">
              <button
                (click)="onDeleteActionClicked(element)"
                mat-icon-button
                aria-label="Button to delete an identity provider"
                matTooltip="Delete identity provider"
              >
                <mat-icon>delete</mat-icon>
              </button>
            </ng-container>
          </div>
        </td>
      </ng-container>

      <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
      <tr mat-row *matRowDef="let row; columns: displayedColumns"></tr>

      <!-- Row shown when there is no data -->
      <tr class="mat-mdc-row mdc-data-table__row" *matNoDataRow>
        <td class="mat-mdc-cell mdc-data-table__cell" [attr.colspan]="displayedColumns.length">No Identity Provider</td>
      </tr>
    </table>
  </gio-table-wrapper>
</mat-card>
